/*
 * @Author: wei_jt@ecidi.com
 * @Date: 2019-11-01 11:28:49
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-30 19:09:31
 * @Description: 路由根配置
 */
import Vue from 'vue'
import Router from 'vue-router'
import { addAlias } from '@/utils/route'

Vue.use(Router)


/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * title: 'title'                 the name show in sidebar and breadcrumb (recommend set)
 * icon: 'svg-name'               the icon show in the sidebar
 * redirect
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
 *  type:                         route/iframe/link
 *  activeMenu: '/Example/list'   the navigation will highlight the path you set                         
  *  query                        
  *  params                       
  *  config: [
  * 'invisible',                  will not in the navigation
  * 'untag',                      will not in the tag bar
  * 'fullScreen',                 page will fill the whole screen; only used to the first level menu
  * 'merge',                      name will be merged in only child case
  * 'noCache',                    the page will no be cached(default is false) used in keep-alive
  * 'affix',                      the tag will affix in the tags-view
  * 'token']                      the page parameter will carry 'fawkes-token'
  }
 */


// 重写Push方法
const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
  let prefix = Vue.prototype.$appBasePath

  if ((typeof location) === 'string') {
    if (!location.includes(prefix))
      location = prefix + location
  }
  if ((typeof location) === 'object' && !location.name) {
    if (!((location.meta && location.meta.keepOrigin) || (location.query && location.query.keepOrigin)) && !location.path.includes(prefix))
      location.path = prefix + location.path
  }
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

//重写Replace方法
const originalReplace = Router.prototype.replace
Router.prototype.replace = function replace (location, onResolve, onReject) {

  let prefix = Vue.prototype.$appBasePath

  if ((typeof location) === 'string') {
    if (!location.includes(prefix))
      location = prefix + location
  }
  if ((typeof location) === 'object' && !location.name) {
    if (!location.path.includes(prefix))
      location.path = prefix + location.path
  }
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}

export const baseRoutes = [{
  path: '/',
  redirect: '/login',
  meta: { config: ['invisible', 'untag'] }
},
{
  path: "/distribute",
  meta: { config: ['fullScreen'] }
},
{
  path: "/default",
  name: 'defualt',
  title: 'HOME',
  component: () => import('@/modules/Home'),
  meta: { config: ['invisible', 'untag'] }
},
//双因子登录页面
{
  path: "/relogin",
  name: 'relogin',
  component: () => import('@/modules/Login/Relogin'),
  meta: { config: ['fullScreen'] },
},
//统一认证登录
{
  path: '/unified',
  name: 'unified',
  component: () => import('@/modules/UnifiedAuthentication/UnifiedLogin/index'),
  meta: { config: ['fullScreen'] },
},
//统一认证-单点登录登录
{
  path: '/testUnified',
  name: 'testUnified',
  component: () => import('@/modules/TestUnifiedAuthentication/UnifiedLogin/index'),
  meta: { config: ['fullScreen'] },
},
//统一认证登录成功跳转
{
  path: '/authenticationSuccess',
  name: 'authenticationSuccess',
  component: () => import('@/modules/UnifiedAuthentication/authenticationSuccess/index'),
  meta: { config: ['fullScreen'] },
},
//回调跳转内部系统示例
{
  path: '/internal',
  name: 'internal',
  component: () => import('@/modules/SystemDemo/Redirect/index'),
  meta: { config: ['fullScreen'] },
},
{
  path: "/activate",
  name: 'activate',
  component: () => import('@/modules/SystemInformation/License/Activate'),
  meta: { config: ['fullScreen'] },
},
{
  path: '/loginPage',
  name: 'loginPage',
  redirect: '/login',
  component: () => import('@/modules/Login'),
  meta: { config: ['fullScreen'] },
  children: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/modules/Login/Login'),
      meta: { config: ['fullScreen'] },
    },
    {
      path: '/forget',
      name: 'forget',
      component: () => import('@/modules/Login/Forget'),
      meta: { config: ['fullScreen'] },
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/modules/Login/Register'),
      meta: { config: ['fullScreen'] },
    },
    {
      path: '/bind',
      name: 'bind',
      component: () => import('@/modules/Login/Bind'),
      meta: { config: ['fullScreen'] },
    },
  ]
},
//个人中心
{
  path: '/personal',
  name: 'personal',
  title: "PERSONAL_CENTER",
  component: () => import('@/modules/PersonalCenter/index.vue'),
  children: [
    {
      path: "profile",
      name: "privacyCenter",
      component: () => import('@/modules/PersonalCenter/PrivacyCenter/index.vue'),
      title: "PERSONAL_CENTER",
    },
    {
      path: "message",
      name: "MessageCenter",
      component: () => import('@/modules/PersonalCenter/MessageCenter/index.vue'),
      title: "MESSAGE_CENTER",
    }]
},
{
  path: '/error',
  name: 'error',
  redirect: '/404',
  component: () => import('@/modules/Error/index.vue'),
  meta: { config: ['fullScreen'] },
  children: [
    {
      name: '404',
      path: '/404',
      component: () => import('@/modules/Error/404.vue'),
      meta: { config: ['fullScreen'] },
    },
    {
      name: '403',
      path: '/403',
      component: () => import('@/modules/Error/403.vue'),
      meta: { config: ['fullScreen'] },
    },
    {
      name: '500',
      path: '/500',
      component: () => import('@/modules/Error/500.vue'),
      meta: { config: ['fullScreen'] },
    }]
}]

//真实作为子应用时会加载的基础路由。即不会加载上面的baseRoutes，而是加载此部分内容
export const microAppRoute = [
]

export function createRouter () {
  //作为子应用运行时不加载基础路由
  let newBaseRoutes = Vue.prototype.$InQianKun ? microAppRoute : baseRoutes

  if (Vue.prototype.$subApp) {
    for (let i = 0; i < newBaseRoutes.length; i++) {
      addAlias(newBaseRoutes[i])
    }
  }
  return new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({
      y: 0
    }),
    routes: [...newBaseRoutes],
    // base:'app'
  })
}

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export function setRoutes (routes) {
  resetRouter()

  if (Vue.prototype.$subApp) {
    for (let i = 0; i < routes.length; i++) {
      addAlias(routes[i])
    }
  }

  router.addRoutes ? router.addRoutes(routes) : router.addRoute(routes)
}


export default router